//
//  LXMusicPlay.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/25.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LXMusicPlay.h"
#import "LXMusicPlayModel.h"
@interface LXMusicPlay ()
@property(nonatomic,retain)STKAudioPlayer *player;
//可以停止
@property(nonatomic,retain)NSTimer *myTimer;
@end
@implementation LXMusicPlay
- (void)dealloc
{
    NSLog(@"ss");
}
+(instancetype)shareLXMusicPlay{
    static LXMusicPlay *sharePlayerVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharePlayerVC = [[LXMusicPlay alloc]init];
        [sharePlayerVC createPlayer];
    });
    return sharePlayerVC;
}
-(void)createPlayer{
    self.player = [[STKAudioPlayer alloc] initWithOptions:(STKAudioPlayerOptions){ .flushQueueOnSeek = YES, .enableVolumeMixer = YES, .equalizerBandFrequencies = {50, 100, 200, 400, 800, 1600, 2600, 16000} }];
}
-(void)play:(NSInteger)musicId{
    NSString *musicUrl = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/track/detail?device=iPhone&trackId=%ld", musicId];
    [AFNetworkTool getUrl:musicUrl body:nil result:HXJSON headerFile:nil success:^(id result) {
        LXMusicPlayModel *model = [[LXMusicPlayModel alloc]init];
        [model setValuesForKeysWithDictionary:result];
        [self.player playURL:[NSURL URLWithString:model.playUrl64]];
    } failure:^(NSError *error) {
        
        
    }];
}
-(void)clearMusic{
    
}
-(NSString*)isPlayOrStop{
    if (self.player.state == STKAudioPlayerStatePlaying) {
        return @"stop";
    }else if(self.player.state == STKAudioPlayerStatePaused){
        return @"continue";
    }else if(self.player.state == STKAudioPlayerStateStopped){
        return @"finish";
    }else{
        return @"start";
    }
}
-(void)stop{
     [self.player pause];
}
-(void)resume{
     [self.player resume];
}
-(NSString *)progress{
    //当前时长progress
    NSInteger proMin = (NSInteger)self.player.progress / 60;//当前秒
    NSInteger proSec = (NSInteger)self.player.progress % 60;//当前分钟
    NSString *currentTime = [NSString stringWithFormat:@"%02ld:%02ld", (long)proMin, (long)proSec];
    if (self.player.progress == self.player.duration) {
        return @"finish";
    }
    return currentTime;
}
-(NSInteger)SliderValue{
    return self.player.progress;
}
-(void)playWithSliderChange:(double)value{
    [self.player seekToTime:value];
}
@end
